import type { LoginResult } from '@/types/member'
import { http, ajhttp } from '@/utils/http'
import { useTenantSystemStore, useMemberStore } from '@/stores'

type LoginWxMinParams = {
  code: string
  encryptedData?: string
  iv?: string
}

/**
 * 账号密码登录
 * @param data 请求参数
 */
export const accountLogin = (data: LoginWxMinParams) => {
  const tenantStore = useTenantSystemStore()
  return ajhttp<LoginResult>({
    method: 'POST',
    url: `/customer/account/login?pid=${tenantStore.state.pid}`,
    data,
  })
}

/**
 * 小程序登录
 * @param data 请求参数
 */
export const postLoginWxMinAPI = (data: LoginWxMinParams) => {
  return http<LoginResult>({
    method: 'POST',
    url: '/login/wxMin',
    data,
  })
}

/**
 * 小程序登录_内测版
 * @param phoneNumber 模拟手机号码
 */
export const postLoginWxMinSimpleAPI = (phoneNumber: string) => {
  return http<LoginResult>({
    method: 'POST',
    url: '/login/wxMin/simple',
    data: {
      phoneNumber,
    },
  })
}

type LoginParams = {
  account: string
  password: string
  inviter?: string
}
/**
 * 手机号快速登录/注册
 * @param data 请求参数
 */
export const postLoginAPI = (data: LoginParams) => {
  const tenantStore = useTenantSystemStore()
  const memberStore = useMemberStore()
  data.inviter = memberStore.profile?.inviter
  return ajhttp<LoginResult>({
    method: 'POST',
    url: `/customer/account/phoneLogin?pid=${tenantStore.state.pid}`,
    data,
  })
}

/**
 * 账户id直接登录
 * @param data 请求参数
 */
export const postUIdLoginAPI = (uid: string) => {
  const tenantStore = useTenantSystemStore()
  return ajhttp<LoginResult>({
    method: 'POST',
    url: `/customer/account/uidLogin?pid=${tenantStore.state.pid}`,
    data: {
      account: uid
    },
  })
}
